package com.color.admin.system.servie.impl;


import com.color.admin.common.core.constant.Constant;
import com.color.admin.system.entity.*;
import com.color.admin.system.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author HaiQiu
 * @date 2021/12/16
 * 项目第一次运行初始化配置
 */
@Component
public class InitServiceImpl implements ApplicationRunner {

    public static final String ADMIN = "admin";

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private DeptRepository deptRepository;

    @Autowired
    private PostRepository postRepository;

    @Autowired
    private MenuRepository menuRepository;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        initSysUser();
    }

    private void initSysUser(){
        SysUser user = userRepository.findByUsername(ADMIN);
        if (ObjectUtils.isEmpty(user)){
            System.out.println("***********************************************\n");
            System.out.println("系统第一次运行");
            SysDept dept = initSysDept();
            SysRole role = initSysRole();
            SysPost post = initSysPost();
            System.out.println("正在初始化系统账户信息");
            SysUser sysUser = new SysUser();
            sysUser.setUsername(ADMIN);
            //角色职位部门
            sysUser.setDept(dept);
            sysUser.setPosts(Collections.singleton(post));
            sysUser.setRoles(Collections.singleton(role));

            sysUser.setEmail("admin@color.com");
            sysUser.setNickname("赠她余香");
            sysUser.setRealName("赠她余香");
            sysUser.setSex(Constant.SEX_SECRET);
            sysUser.setAvatar("https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png");
            sysUser.setPhone("15188888888");
            sysUser.setActive(Constant.ACTIVE_ENABLE);
            sysUser.setStatus(Constant.STATUS_ENABLE);
            sysUser.setSort(0);
            sysUser.setPassword(ADMIN);
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            userRepository.save(sysUser);
            System.out.println("创建管理员admin成功：账号密码均为："+ADMIN+"\n");
            System.out.println("***********************************************");
        }
    }

    private SysDept initSysDept(){
        String name = "科技集团";
        SysDept dept = deptRepository.findSysDeptByDeptName(name);
        if (ObjectUtils.isEmpty(dept)){
            System.out.println("正在初始化系统部门信息");
            SysDept sysDept = new SysDept();
            sysDept.setDeptName(name);
            sysDept.setDescription("初始化部门");
            sysDept.setParentId("0");
            sysDept.setActive(Constant.ACTIVE_ENABLE);
            sysDept.setSort(0);
            sysDept.setCreateTime(new Date());
            sysDept.setUpdateTime(new Date());
            SysDept save = deptRepository.save(sysDept);
            System.out.println("创建默认部门成功");
            System.out.println("***********************************************");
            return save;
        }
        return null;
    }

    private SysRole initSysRole(){
        String name = "管理员";
        SysRole role = roleRepository.findByRoleName(name);
        if (ObjectUtils.isEmpty(role)){
            System.out.println("正在初始化系统角色信息");
            SysRole sysRole = new SysRole();
            sysRole.setRoleName(name);
            sysRole.setDescription("系统管理员角色");
            sysRole.setActive(Constant.ACTIVE_ENABLE);
            sysRole.setSort(0);
            sysRole.setCreateTime(new Date());
            sysRole.setUpdateTime(new Date());
            //设置菜单权限
//            sysRole.setMenus(Collections.singleton(initMenu()));
            SysRole save = roleRepository.save(sysRole);
            System.out.println("创建默认角色成功");
            System.out.println("***********************************************");
            return save;
        }
        return null;
    }


    private SysPost initSysPost(){
        String name = "董事长";
        SysPost post = postRepository.findByPostName(name);
        if (ObjectUtils.isEmpty(post)){
            System.out.println("正在初始化系统职位信息");
            SysPost sysPost = new SysPost();
            sysPost.setPostName(name);
            sysPost.setDescription("集团老板，执行董事会");
            sysPost.setActive(Constant.ACTIVE_ENABLE);
            sysPost.setSort(0);
            sysPost.setCreateTime(new Date());
            sysPost.setUpdateTime(new Date());
            SysPost save = postRepository.save(sysPost);
            System.out.println("创建默认职位成功");
            System.out.println("***********************************************");
            return save;
        }
        return null;
    }

    private SysMenu initMenu(){
        String name = "系统";
        List<SysMenu> menus = menuRepository.findSysMenuByMenuName(name);
        if (CollectionUtils.isEmpty(menus)){
            System.out.println("正在初始化权限菜单信息");
            SysMenu sysMenu = new SysMenu();
            sysMenu.setMenuName(name);
            sysMenu.setComponent("/view/user/index.vue");
            sysMenu.setPath("/");
            sysMenu.setParentId("0");
            sysMenu.setUpdateTime(new Date());
            sysMenu.setCreateTime(new Date());
            sysMenu.setSort(0);
            sysMenu.setType("menu");
            sysMenu.setHidden(false);
            sysMenu.setActive(true);
            sysMenu.setDescription("系统权限菜单");
            sysMenu.setValue("system");
            SysMenu save = menuRepository.save(sysMenu);
            System.out.println("创建默认权限菜单成功");
            System.out.println("***********************************************");
            return save;
        }
        return null;
    }

}
